﻿using GalaSoft.MvvmLight;
using IkeCode.WP7.HQReader.Base;
using IkeCode.WP7.HQReader.Interfaces;
using System.Windows.Input;
using IkeCode.WP7.Toolkit.Base;
using GalaSoft.MvvmLight.Messaging;
using IkeCode.WP7.HQReader.Model;
using System.Windows;
using System;
using IkeCode.WP7.Toolkit;

namespace IkeCode.WP7.HQReader.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can databind to.
    /// </summary>
    public class PublisherManagerViewModel : BaseViewModel
    {
        #region ## Services ##

        private readonly IFeedPublisherDataService _publisherDataService;

        #endregion ## Services ##

        #region ## Properties ##

        /// <summary>
        /// 
        /// </summary>
        public override Uri MyUri
        {
            get
            {
                return new Uri("/Views/PublisherManagerPage.xaml", UriKind.RelativeOrAbsolute);
            }
        }

        /// <summary>
        /// Publisher model
        /// </summary>
        private IIcFeedPublisher Publisher { get; set; }

        /// <summary>
        /// Is update flag
        /// </summary>
        private bool IsUpdate { get; set; }

        private string _title;
        /// <summary>
        /// Publisher Title
        /// </summary>
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        private Uri _url;
        /// <summary>
        /// Publisher Url
        /// </summary>
        public Uri Url
        {
            get { return _url; }
            set
            {
                _url = value;
                RaisePropertyChanged("Url");
            }
        }


        #endregion ## Properties ##

        /// <summary>
        /// Initializes a new instance of the PublisherManagerViewModel class.
        /// </summary>
        public PublisherManagerViewModel(IFeedPublisherDataService publisherDataService)
        {
            _publisherDataService = publisherDataService;

            Messenger.Default.Register<IIcFeedPublisher>(this, (publisher) =>
            {
                this.IsUpdate = publisher != null;

                if (this.IsUpdate)
                {
                    this.Publisher = publisher;
                    this.Title = this.Publisher.Title;
                    this.Url = this.Publisher.Url;
                    this.PageTitle = string.Format("editing {0}", this.Publisher.Title);
                }
                else
                {
                    this.PageTitle = "adding...";
                }
            });
        }

        #region ## Commands ##

        /// <summary>
        /// Command to save publisher edit/add
        /// </summary>
        public ICommand SaveCommand
        {
            get
            {
                return new RelayCommand((param) =>
                {
                    var publisher = this.IsUpdate ? this.Publisher : new IcFeedPublisherModel();
                    publisher.Title = this.Title;
                    publisher.Url = this.Url;
                    _publisherDataService.PostPublisher(publisher, (publisherResult, error) =>
                    {
                        if (error != null)
                        {
                            this.ShowMessage(error.Message);
                        }
                        else
                        {
                            if (this.IsUpdate)
                                this.ShowMessage("Publisher successfully updated!");
                            else
                                this.ShowMessage("Publisher successfully added!");

                            this.PageTitle = string.Format("editing {0}", publisher.Title);
                            Messenger.Default.Send<MainViewModel>(null, "Reload");
                        }
                    });
                });
            }
        }

        /// <summary>
        /// Command to remove selected publisher
        /// </summary>
        public override ICommand RemoveCommand
        {
            get
            {
                return new RelayCommand((param) =>
                {
                    if (param != null)
                    {
                        IIcFeedPublisher publisher = (IIcFeedPublisher)param;
                        if (publisher != null)
                        {
                            _publisherDataService.DeletePublisher(publisher, (success, error) =>
                            {
                                if (error != null)
                                {
                                    this.ShowMessage("Error occurred!");
                                }
                                else if (success)
                                {
                                    this.ShowMessage("Publisher successfully deleted!");
                                    Messenger.Default.Send<MainViewModel>(null, "Reload");
                                    this._navigationService.GoBack();
                                }
                            }, true);

                        }
                    }
                });
            }
        }

        #endregion ## Commands ##
    }
}